package com.tws.pctask.service;

import com.tws.common.model.ResponseWrapper;
import com.tws.common.tool.CommonTool;
import com.tws.common.tool.DataProcessing;
import com.tws.dao.entity.User;
import com.tws.pctask.mapper.StaffManageMapper;
import com.tws.pctask.model.registerManageModel.CreatTables;
import com.tws.pctask.model.staffManageModel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StaffManageService {
    /**
     * 获取当前登录用户的用户信息
     * 并根据id生成表名
     */
    User loginUser = LoginService.getLoginUser();
    CreatTables tables = new CreatTables(loginUser.getUserId());

    @Autowired
    StaffManageMapper staffManageMapper;

    /**
     * 6.1获取员工列表
     * @param param
     * @return
     */
    public ResponseWrapper getStaffList(GetStaffListParam param){
        param.setTableName("02044f2fcd41_staff");
        param.setSearchKey(DataProcessing.CreatFuzzyQuery(param.getSearchKey()));
        param.setPage(DataProcessing.CalculatePage(param.getPage(), param.getPageSize()));
        List<Map<String, Object>> mapList = staffManageMapper.GetStaffList(param);
        List<GetStaffListResult> results = CommonTool.mapListToBeanList(mapList, GetStaffListResult.class);
        return ResponseWrapper.markSuccess(results);
    }
    /**
     * 6.2新增或修改员工信息
     * @param param
     * @return
     */
    public ResponseWrapper addOrUpdateStaff(AddOrUpdateStaffParam param){
        param.setTableName("02044f2fcd41_staff");
        staffManageMapper.addOrUpdateStaff(param);
        return ResponseWrapper.markSuccess();
    }
    /**
     * 6.3检查员工姓名是否重复
     * @param param
     * @return
     */
    public ResponseWrapper checkStaffName(CheckStaffNameParam param){
        param.setTableName("02044f2fcd41_staff");
        Integer count = staffManageMapper.checkStaffName(param);
        boolean isExist = count > 0;
        return ResponseWrapper.markSuccess(new CheckStaffNameResult(isExist));
    }
    /**
     * 6.4获取单个员工信息
     * @param param
     * @return
     */
    public ResponseWrapper getStaffInfo(GetStaffInfoParam param){
        param.setTableName("02044f2fcd41_staff");
        Map<String, Object> staffInfo = staffManageMapper.getStaffInfo(param);
        List<Map<String,Object>> mapList = new ArrayList<>();
        mapList.add(staffInfo);
        List<GetStaffInfoResult> results = CommonTool.mapListToBeanList(mapList,GetStaffInfoResult.class);
        return ResponseWrapper.markSuccess(results.get(0));
    }
    /**
     * 6.5删除员工
     * @param param
     * @return
     */
    public ResponseWrapper delStaff(DelStaffParam param){
        param.setTableName("02044f2fcd41_staff");
        staffManageMapper.delStaff(param);
        return ResponseWrapper.markSuccess();
    }
}
